package com.dazzle.core.util.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.dazzle.core.util.query.PagingBean;

@SuppressWarnings("serial")
public class PageTag extends TagSupport {
	private PagingBean pagingBean;

	@Override
	public int doStartTag() throws JspException {
		try {
            JspWriter out = this.pageContext.getOut();
            Integer curPage = pagingBean.getCurrentPage();
            Integer pages = pagingBean.getTotalPages();
            
            StringBuffer buff = new StringBuffer("<div class=\"pagination pagination-right pull-right\" style=\"display:inline;\"><ul>");
            if(curPage !=null && curPage == 1){
            	buff.append("<li class=\"disabled\"><a>&laquo;</a></li>");
            	buff.append("<li class=\"active\"><a>1</a></li>");
            }else{
            	buff.append("<li><a onclick=\"clickPage("+(curPage-1)+");\">&laquo;</a></li>");
            	buff.append("<li><a onclick=\"clickPage("+1+");\">1</a></li>");
            }
            
            int pagesInBody = pages-2;
            int displayPage = 0;
            if(pagesInBody >0 && pagesInBody<8){
            	for(int i=0;i<pagesInBody;i++){
            		displayPage = (2+i);
            		buff.append("<li><a onclick=\"clickPage("+displayPage+");\">"+displayPage+"</a></li>");
            	}
            }
            if(pagesInBody>8){
            	if(curPage<=6){
            		for(int i=0;i<8;i++){
            			displayPage = (2+i);
            			if(displayPage == curPage){
            				buff.append("<li  class=\"active\"><a>"+displayPage+"</a></li>");
            				continue;
            			}
            			buff.append("<li><a onclick=\"clickPage("+displayPage+");\">"+displayPage+"</a></li>");
            		}
            		buff.append("<li><a>...</a></li>");
            	}else{
            		buff.append("<li><a>...</a></li>");
            		for(int i=0;i<4;i++){
            			displayPage = (curPage-4+i);
            			buff.append("<li><a onclick=\"clickPage("+displayPage+");\">"+displayPage+"</a></li>");
            		}
            		buff.append("<li class=\"active\"><a>"+curPage+"</a></li>");
            		
            		if(pages-curPage>5){
            			for(int i=0;i<4;i++){
            				displayPage = (curPage+1+i);
            				buff.append("<li><a onclick=\"clickPage("+displayPage+");\">"+displayPage+"</a></li>");
            			}
            			buff.append("<li><a>...</a></li>");
            		}else{
            			for(int i=0;i<pages-curPage-1;i++){
            				displayPage = (curPage+1+i);
            				buff.append("<li><a onclick=\"clickPage("+displayPage+");\">"+displayPage+"</a></li>");
            			}
            		}
            	}
            }
            
            
            if(pages > curPage){
            	buff.append("<li><a onclick=\"clickPage("+pages+");\">"+pages+"</a></li>");
            }
            if(curPage == pages){
            	if(pages==2){
            		buff.append("<li class=\"active\"><a>"+pages+"</a></li>");
            	}
            	buff.append("<li class=\"disabled\"><a>&raquo;</a></li>");
            }else{
            	buff.append("<li><a onclick=\"clickPage("+(curPage+1)+");\">&raquo;</a></li>");
            }
            buff.append("<input type=\"text\" style=\"padding-left:2px;height:30px;width:40px;\">");
            buff.append("</ul></div>");
            buff.append("<br><input type=\"hidden\" id=\"start\" name=\"start\" value=\""+pagingBean.getStart()+"\">");
            buff.append("<script type=\"text/javascript\">function clickPage(curPage){$('#start').val((curPage-1)*"+pagingBean.getPageSize()+"); $('#formForList').submit();}</script>");
            out.println(buff.toString());
        } catch(Exception e) {
            throw new JspException(e.getMessage());
        }
        return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	@Override
	public void release() {
		super.release();
		this.pagingBean = null;
	}

	public PagingBean getPagingBean() {
		return pagingBean;
	}

	public void setPagingBean(PagingBean pagingBean) {
		this.pagingBean = pagingBean;
	}
	
}
